package com.atguigu.day73_demo;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

class MyThread implements Callable<String> {
    @Override
    public String call() throws Exception {
        System.out.println(Thread.currentThread().getName() + "====");

        return "hello";
    }
}


/**
 * @auther zzyy
 * @create 2024-08-06 16:47
 * FutureTask(Runnable runnable, V result)创建一个 FutureTask，它将在运行时执行给定的 Runnable，并安排 get 在成功完成时返回给定的结果。
 */
public class CallableDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        MyThread myThread = new MyThread();

        FutureTask<String> futureTask = new FutureTask<String>(myThread);
        Thread thread = new Thread(futureTask, "A");
        thread.start();

       // FutureTask<String> futureTask2 = new FutureTask<String>(myThread);
        Thread thread2 = new Thread(futureTask, "B");
        thread2.start();



        System.out.println(Thread.currentThread().getName()+"mian");
        System.out.println(futureTask.get());
        // System.out.println(futureTask.get());
    }
}
